package com.regent.central.bill.web;

import com.regent.central.infrastructure.web.BasicResponse;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;

/**
 * 异常拦截器
 *
 * @author 杨昌沛 646742615@qq.com
 * @date 2018-07-04 13:40
 **/
@Aspect
public class ExceptionInterceptor {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Pointcut("execution(* com.regent.central.bill.web.delivery.*Controller.*(..)) || execution(* com.regent.central.bill.web.sale_order.*Controller.*(..))")
    public void pointcut() {
    }

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        try {
            return point.proceed();
        } catch (Throwable e) {
            logger.error(e.getMessage(), e);
            MethodSignature signature = (MethodSignature) point.getSignature();
            Method method = signature.getMethod();
            BasicResponse response = (BasicResponse) method.getReturnType().newInstance();
            response.setCode(500);
            response.setMessage(e.getMessage());
            return response;
        }
    }

}
